package com.itheima.service.impl;

import com.itheima.mapper.DetMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DetMapper detMapper;



    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Dept> finAll() {


        return detMapper.findAll();
    }


    /**
     * 删除部门
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //检查部门下是否有员工
        int empCount = detMapper.countEmpByDeptId(id);
        if (empCount >0) {
            throw new RuntimeException("对不起，当前部门下有员工，不能直接删除！");
        }
        detMapper.deleteById(id);
    }

    /**
     * 添加部门
      * @param dept
     */
    @Override
    public void add(Dept dept) {
        // 设置创建时间
        dept.setCreateTime(LocalDateTime.now());
        // 设置修改时间
        dept.setUpdateTime(LocalDateTime.now());
        detMapper.add(dept);
    }

    /**
     * 根据id查询部门
     * @param id
     * @return
     */
    @Override
    public Dept deptService(Integer id) {

        return detMapper.getByid(id);
    }

    /**
     * 修改部门
     * @param dept
     */
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        detMapper.update(dept);

    }





}
